<?php
require_once 'GlobalConstant.php';
require_once 'UserAgent.php';
require_once 'XUCBrowserUserAgent.php';

/**
 * <b>UserAgentFactory简介:</b>
 * <p>通过HTTP Header User-Agent，X-UCBrowser-UA字段生成对应的对象模型</p>
 *
 * <b>功能描述:</b>
 * <p>相关的字符串分析等功能</p>
 *
 * @author cairq@ucweb.com
 */
class UserAgentFactory {
    public static function createUserAgent($userAgent){
        return UserAgentFactory::parseUserAgent($userAgent);
    }

    public static function createXUCBrowserUserAgent($xUCBrowserUserAgent){
        return UserAgentFactory::parseXUCBrowserUserAgent($xUCBrowserUserAgent);
    }
    
    /**
     * 
     * <p>1. 解析XUCBrowserUserAgent</p>
     * <p>2. 对数据进行封装为对象模型</p>
     * <p>格式： Key1(Value1);Key2(Value2);...</p>
     * <p>例如：例如： pf(Java);er(U);la(zh-CN);up(U2/1.0.0);re(U2/1.0.0);dv(Nokia6300);pr(UCBrowser/8.3.0.182);ov(S40 V3);pi(320*240);de(320*240);pm(1);bv(1);nm(0);im(1);sr(2);</p>
     * @param string $xUCBrowserUserAgent
     * @return XUCBrowserUserAgent
     */
    public static function parseXUCBrowserUserAgent($xUCBrowserUserAgent){
        $ua = new XUCBrowserUserAgent();
        
        // ua 为null标识ua不可用
        if(is_null($xUCBrowserUserAgent) || trim($xUCBrowserUserAgent) == ''){
            $ua->setEmpty(true);
            $ua->setUnkown(true);
            return $ua;
        }
        
        try{
            $ua->setxUCBrowserUserAgent($xUCBrowserUserAgent);
            $keyValueItems = explode(";", $xUCBrowserUserAgent);
            
            $keyValueItemsLength = count($keyValueItems);
            for($j = 0; $j < $keyValueItemsLength; $j++){
                $text = $keyValueItems[$j];
                if(empty($text)){
                    continue;
                }
                $startIndex = strpos($text, "(");
                $endIndex = strpos($text, ")");
                if($startIndex === false || $endIndex === false){
                    $ua->setUnkown(true);
                    return $ua;
                }
                $key = strtolower(substr($text, 0, $startIndex));
                $value = substr($text, $startIndex + 1, $endIndex - $startIndex - 1);

                if($key == "pf"){
                    $ua->setPlatform($value);
                }else if($key == "la"){
                    $ua->setLanguage($value);
                }else if($key == "er"){
                    $ua->setEncryption($value);
                }else if($key == "re"){
                    $index = strpos($value, "/");
                    $ua->setRenderingEngine(substr($value, 0, $index));
                    $ua->setRenderingEngineVersion(substr($value, $index + 1));  
                }else if($key == "up"){
                    $index = strpos($value, "/");
                    $ua->setUcPlatform(substr($value, 0, $index));
                    $ua->setUcPlatformVersion(substr($value, $index + 1));  
                }else if($key == "dv"){
                    $ua->setDevice($value);
                }else if($key == "pr"){
                    $index = strpos($value, "/");
                    $ua->setProduct(substr($value, 0, $index));
                    $ua->setProductVersion(substr($value, $index + 1));              
                }else if($key == "ov"){
                    $index = strpos($value, " ");
                    $ua->setOs(substr($value, 0, $index));
                    $ua->setOsVersion(substr($value, $index + 1));        
                }else if($key == "pi"){
                    $ua->setPixel($value);
                }else if($key == "de"){
                    $ua->setDensity($value);
                }else if($key == "pm"){
                    $ua->setProxyMode($value);
                }else if($key == "bv"){
                    $ua->setBrowsingView($value);
                }else if($key == "nm"){
                    $ua->setNightMode($value);
                }else if($key == "im"){
                    $ua->setNoImageMode($value);
                }else if($key == "sr"){
                    $ua->setScreenRotation($value);
                }else if($key == "nt"){
                    $ua->setNetType($value);
                }else if($key == "bt"){
                    $ua->setBtype($value);
                }
            }
        } catch (Exception $e){
            error_log($e->getMessage());
        }
        return $ua;
    }
    
    /**
     * 
     * <p>解析user-agent字段，分析为标准对象模型</p>
     * 
     * <p>新UA自有内核规范：Company/Version (Platform; Encryption; OS Version; Language; Device) Rendering Engine/Version (Rendering Details) Product/Version UC Platform/Version Extensions</p>
     * <p>例如：                        Mozilla/5.0(Linux; U; Android 2.3; zh-CN; MI-ONEPlus) AppleWebKit/534.13 (KHTML, like Gecko) UCBrowser/8.6.0.199 U3/0.8.0 Safari/534.13</p>
     * <p>新UA自带内核规范：OBUA Product/Version UC Platform/Version Extensions。其中OBUA为自带浏览器UA</p>
     * <p>例如：                        Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5 UCBrowser/8.6.0.199 U2/1.0.0 Mobile</p>
     * <p>例如：                        Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; Microsoft; XDeviceEmulator) UCBrowser/8.6.0.199 U2/1.0.0 Mobile"</p>
     * <p>旧UA规范：               Device UA/UCWEB Version/PFID/BID。Devic UA为设备型号</p>
     * <p>例如：　　                NOKIA E71 RM-346/UCWEB6.7.1.1/28/999</p>
     * <p></p>
     *
     * @param string $userAgent
     * @return UserAgent
     */
    public static function parseUserAgent($userAgent){
        $ua = new UserAgent();
        // ua 为null标识ua不可用
        if(is_null($userAgent) || trim($userAgent) == ''){
            $ua->setEmpty(true);
            $ua->setUnkown(true);
            return $ua;
        }
        
        $ua->setUserAgent($userAgent); // 设置HTTP Header最原始的User-Agent
        
        //$userAgentTemp = strtolower(userAgent);
        // 智能机自带浏览器内核 OBUA Product/Version U2/1.0.0 Mobile
        if(stripos($userAgent, GlobalConstant::U2) !== false && stripos($userAgent, GlobalConstant::WEBKIT) !== false){
            UserAgentFactory::parseU2AndU3($userAgent, $ua); 
        }
        
        //兼容IE自带内核的UA情况
        else if(stripos($userAgent, GlobalConstant::U2) !== false && stripos($userAgent, GlobalConstant::MSIE) !== false){
            UserAgentFactory::parseMsieUA($userAgent, $ua);
        }
        
        //最新标准UA的处理方式
        //UCWEB/2.0(iOS; U; iPh OS 4_3_2; zh-CN; iPh4) U2/1.0.0  UCBrowser/8.6.0.199 U2/1.0.0 Mobile 
        //Mozilla/5.0(Linux; U; Android 2.3; zh-CN; MI-ONEPlus) AppleWebKit/534.13 (KHTML, like Gecko) UCBrowser/8.6.0.199 U3/0.8.0 Safari/534.13
        else if(stripos($userAgent, GlobalConstant::U2) !== false || stripos($userAgent, GlobalConstant::U3) !== false){           
            UserAgentFactory::parseU2AndU3($userAgent, $ua);            
        }
        
        // 旧有平台，Android，iPhone,WP的处理。
        else if(stripos($userAgent, GlobalConstant::J2ME) !== false){
            $ua->setVersion(GlobalConstant::VERSION_1);
            $ua->setProduct(GlobalConstant::UCBROWSER);
            $ua->setUcPlatform(GlobalConstant::U2);
            $ua->setUcPlatformVersion("1.0.0");
        } 
        // JUC (Linux;U;AndriodVersion;Language;Model;ScreenWide*ScreenHeight;) Browser Name+Browser Version/PFID/BID
        else if(stripos($userAgent, GlobalConstant::JUC) !== false){            
           UserAgentFactory::parseJUC($userAgent, $ua);    
        }
        else if(stripos($userAgent, GlobalConstant::IUC ) !== false){
            UserAgentFactory::parseIUC($userAgent, $ua);    
        }else if(stripos($userAgent, GlobalConstant::UCBROWSER ) !== false){
            UserAgentFactory::parseOldUCBrowserPlatform($userAgent, $ua);
        } else if(stripos($userAgent, GlobalConstant::_UC_BROWSER ) !== false && stripos($userAgent, GlobalConstant::MOBILE ) === false){
            UserAgentFactory::parseOldUC_BrowserPlatform($userAgent, $ua);
        } else if(stripos($userAgent, GlobalConstant::_UC_BROWSER ) !== false){
            UserAgentFactory::parseOldUCBrowserPlatform($userAgent, $ua);
        } else if(stripos($userAgent, GlobalConstant::UCWEB ) !== false && stripos($userAgent, GlobalConstant::MOBILE ) !== false){
            UserAgentFactory::parseOldUCBrowserPlatform($userAgent, $ua);
        }
        // 旧有平台的UA处理 : NOKIA E71 RM-346/UCWEB6.7.1.1/28/999
        else if(stripos($userAgent, GlobalConstant::UCWEB) !== false){
            UserAgentFactory::parseOldPlatform($userAgent, $ua);
        } else if(stripos($userAgent, GlobalConstant::_UC ) !== false){
            UserAgentFactory::parseOldUCBrowserPlatform($userAgent, $ua);
        }
        // 其他非UC浏览器的UA识别，交给第3方公司自己内部处理吧。
        else{
            $ua->setUnkown(true); 
        }   
        return $ua;
    }
    
    private static function parseOldUC_BrowserPlatform($userAgent, $ua) {
    	$ua->setVersion(GlobalConstant::VERSION_1);
    	$values = explode("/", $userAgent);
    	$ua->setDevice($values[0]);
    	$ua->setProduct(GlobalConstant::UCBROWSER);
    	$index = 0;
        $valueCount = count($values);
    	for($i = 0; $i < $valueCount; $i++){
    		$index = stripos($values[$i], GlobalConstant::_UC_BROWSER);
    		if( $index !== false){
    			$index = $index + strlen(GlobalConstant::_UC_BROWSER);
    			$ua->setProductVersion(trim(substr($values[$i], $index)));
    			break;
    		}
    	}
    }

    /**
     * 设置os信息
     * @param type $userAgent
     * @param type $os
     * @return type
     */
    private static function setOsInfo($ua, $os){
        if(empty($os)){
            return;
        }
        $osItem = explode(" ", $os);
        // 如果符合UC浏览器User-Agent最新规范是可以分析出版本号，否则无法分析版本号
        if (count($osItem) == 2) {
            $ua->setOs($osItem[0]);
            $ua->setOsVersion($osItem[1]);
        } else {
            $index = 0;
            //查找数字
            $osCount = strlen($os);
            for (; $index < $osCount && !is_numeric(substr($os, $index, 1)); $index++);
            if ($index === $osCount) {
                $ua->setOs(trim($os));
            } else {
                $ua->setOs(trim(substr($os, 0, $index)));
                $ua->setOsVersion(trim(substr($os, $index)));
            }
        }
    }
    
    private static function parseOldUCBrowserPlatform($userAgent, $ua) {
        $ua->setVersion(GlobalConstant::VERSION_1);
        $ua->setProduct(GlobalConstant::UCBROWSER);
        $companyVersion = "";
        $platform = "";
        $encryption = "";
        $os = "";
        $language = "";
        $device = "";
        $rendering = "";
        $product = "";
        $mobile = "";
        $other = "";
        
        $userAgent = trim($userAgent);
        $mozillaIndex = stripos($userAgent, GlobalConstant.MOZILLA);
        if($mozillaIndex !== false){ //兼容手机底层会自动在我们的UA前面加上系统UA的问题
            $userAgent = substr($userAgent, $mozillaIndex);
        }
        
        $startIndex = strpos($userAgent, "(");
        
        // 获取Company参数
        if($startIndex !== false){
            $companyVersion = substr($userAgent, 0, $startIndex);
        }
        
        $endIndex = strpos($userAgent, ")");
        
        $deviceInfo =  substr($userAgent, $startIndex + 1, $endIndex - $startIndex - 1);
        $deviceInfos = explode(";", $deviceInfo);
        try{
            $platform = trim($deviceInfos[0]);
            $ua->setPlatform($platform);
            
            $encryption = trim($deviceInfos[1]);
            $ua->setEncryption($encryption);
            
            $index = 2;
            
            if(count($deviceInfos) > 4){
                $os = trim($deviceInfos[$index++]);
                UserAgentFactory::setOsInfo($ua, $os);
            }
            
            $language = trim($deviceInfos[$index++]);
            $ua->setLanguage($language);
            
            $device = trim($deviceInfos[$index++]);
            $ua->setDevice($device);
        } catch (Exception $e) {
            error_log($e->getMessage());
        }
        
        try{
            $desc = trim(substr($userAgent, $endIndex + 1));
            $descs = explode(" ", $desc);

            $index = -1;
             
            if(stripos($desc, "(") !== false){
                $rendering = trim($descs[0]);
                if(strtolower(GlobalConstant::_UC) == strtolower($rendering)){
            		$rendering = trim($descs[1]);
            	}
                
                $index = strpos($rendering, "/");
                $ua->setRenderingEngine(substr($rendering, 0, $index));
                $endIndex = stripos($rendering, "(");
                if($endIndex !== false){
                    $ua->setRenderingEngineVersion(substr($rendering, $index + 1, $endIndex - $index - 1));
                } else {
                    $ua->setRenderingEngineVersion(substr($rendering, $index + 1));
                }
            }
            
            $length = strlen(GlobalConstant::UCBROWSER);
            $index = strpos($desc, GlobalConstant::UCBROWSER);
            if($index === false){
                $length = strlen(GlobalConstant::UCWEB);
                $index = strpos($desc, GlobalConstant::UCWEB);
            }
            if($index !== false){
            	$startVerIndex = $index + $length;
            	if(substr($desc, $startVerIndex, 1) == "/"){
                    $endIndex = stripos($desc, "/", $startVerIndex + 1);
                    $startVerIndex = $startVerIndex + 1;
            	} else {
                    $endIndex = stripos($desc, "/", $index + 1);
            	}
            	if($endIndex !== false){
            		$ua->setProductVersion(trim(substr($desc, $startVerIndex, $endIndex - $startVerIndex)));
            	}
            }
            
            $descsLength = count($descs);
            for($j=1; $j < $descsLength; $j++){ 
                if(stripos($descs[$j], GlobalConstant::U2) !== false || stripos($descs[$j], GlobalConstant::U3) !== false){
                    $item = trim($descs[$j]);
                    $index = strpos($item, "/");
                    $ua->setUcPlatform(substr($item, 0, $index));
                    $ua->setUcPlatformVersion(substr($item, $index + 1));
                }else if(stripos($descs[$j], GlobalConstant::MOBILE) !== false){
                    $ua->setMobileMode(true);
                }
            }
        }catch (Exception $e) { 
            error_log($e->getMessage());
        }
    }
    
    
    private static function parseOldPlatform($userAgent, $ua) {
        $ua->setVersion(GlobalConstant::VERSION_1);
        $startIndex = strpos($userAgent, "/");
        
        $device = substr($userAgent, 0, $startIndex);
        $ua->setDevice($device);
        $ua->setProduct(GlobalConstant::UCBROWSER);
        $endIndex = strpos($userAgent, "/", $startIndex + 1);
        $ua->setProductVersion(substr($userAgent, $startIndex + 6, $endIndex - $startIndex - 6));
        $ua->setUcPlatform(GlobalConstant::U2);
        $ua->setUcPlatformVersion("1.0.0");
    }

    private static function parseU2AndU3($userAgent, $ua) {
        $ua->setVersion(GlobalConstant::VERSION_2);
        //$ua->setVersion(GlobalConstant::VERSION_2);
        $companyVersion = "";
        $platform = "";
        $encryption = "";
        $os = "";
        $language = "";
        $device = "";
        $rendering = "";
        $product = "";
        $mobile = "";
        $other = "";
        
        $userAgent = trim($userAgent);
        $UCWEB_2_0Index = stripos($userAgent, "UCWEB/2.0");
        if($UCWEB_2_0Index !== false){ //兼容手机底层会自动在我们的UA前面加上系统UA的问题
            $userAgent = substr($userAgent, $UCWEB_2_0Index);
        }
        
        $startIndex = strpos($userAgent, "(");
        
        // 获取Company参数
        if($startIndex !== false){
            $companyVersion = substr($userAgent, 0, $startIndex);
        }
        
        $endIndex = strpos($userAgent, ")");
        
        $deviceInfo =  substr($userAgent, $startIndex + 1, $endIndex - $startIndex - 1);
        $deviceInfos = explode(";", $deviceInfo);
        try{
            $platform = trim($deviceInfos[0]);
            $ua->setPlatform($platform);
            
            $encryption = trim($deviceInfos[1]);
            $ua->setEncryption($encryption);
            
            $os = trim($deviceInfos[2]);
            UserAgentFactory::setOsInfo($ua, $os);
            
            $language = trim($deviceInfos[3]);
            $ua->setLanguage($language);
            
            if(count($deviceInfos) >= 5){
                $device = trim($deviceInfos[4]);
                $ua->setDevice($device);
            }else{
                
            }
        }catch (Exception $e) { 
            error_log($e->getMessage());
        }
        
        try{
            $desc = trim(substr($userAgent, $endIndex + 1));
            $descs = explode(" ", $desc);

            $rendering = trim($descs[0]);
            $index = strpos($rendering, "/");
            $ua->setRenderingEngine(substr($rendering, 0, $index));
            $ua->setRenderingEngineVersion(substr($rendering, $index + 1));
            
            $descsLength = count($descs);
            for($j=1; $j < $descsLength; $j++){ 
                if(stripos($descs[$j], GlobalConstant::UCBROWSER) !== false){
                    $product = trim($descs[$j]);
                    $index = strpos($product, "/");
                    $ua->setProduct(substr($product, 0, $index));
                    $ua->setProductVersion(substr($product, $index + 1));
                }else if(stripos($descs[$j], GlobalConstant::U2) !== false || stripos($descs[$j], GlobalConstant::U3) !== false){
                    $item = trim($descs[$j]);
                    $index = strpos($item, "/");
                    $ua->setUcPlatform(substr($item, 0, $index));
                    $ua->setUcPlatformVersion(substr($item, $index + 1));
                }else if(stripos($descs[$j], GlobalConstant::MOBILE) !== false){
                    $ua->setMobileMode(true);
                }
            }
        }catch (Exception $e) { 
            error_log($e->getMessage());
        }
    }
    
    //Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; Microsoft; XDeviceEmulator)UCBrowser/8.6.0.199 U2/1.0.0 Mobile
    private static function parseMsieUA($userAgent, $ua) {
        $ua->setVersion(GlobalConstant::VERSION_2);
        $companyVersion = "";
        $platform = "";
        $encryption = "";
        $os = "";
        $language = "";
        $device = "";
        $rendering = "";
        $product = "";
        $mobile = "";
        $other = "";
        $startIndex = strpos($userAgent, "(");
        
        // 获取Company参数
        if($startIndex !== false){
            $companyVersion = substr($userAgent, 0, $startIndex);
        }
        
        $endIndex = strpos($userAgent, ")");
        
        $deviceInfo =  substr($userAgent, $startIndex + 1, $endIndex - $startIndex - 1);
        $deviceInfos = explode(";", $deviceInfo);
        $deviceInfosLength = count($deviceInfos);
        try{
            $platform = trim($deviceInfos[2]);
            $ua->setPlatform($platform); 
            $ua->setOs($platform);   
         
            $rendering = trim($deviceInfos[3]);
            $index = strpos($rendering, "/");
            $ua->setRenderingEngine(substr($rendering, 0, $index));
            $ua->setRenderingEngineVersion(substr($rendering, $index + 1));
            
            if($deviceInfosLength > 7){
                $device = trim($deviceInfos[7]);
                $ua->setDevice($device);  
            }
        }catch (Exception $e) { 
            error_log($e->getMessage());
        }
        
        try{
            $desc = trim(substr($userAgent, $endIndex + 1));
            $descs = explode(" ", $desc);

            $index = -1;
            
            $descsLength = count($descs);
            
            for($j = 0; $j < $descsLength; $j++){ 
                if(stripos($descs[$j], GlobalConstant::UCBROWSER) !== false){
                    $product = trim($descs[$j]);
                    $index = strpos($product, "/");
                    $ua->setProduct(substr($product, 0, $index));
                    $ua->setProductVersion(substr($product, $index + 1));
                }else if(stripos($descs[$j], GlobalConstant::U2) !== false || stripos($descs[$j], GlobalConstant::U3) !== false){
                    $item = trim($descs[$j]);
                    $index = strpos($item, "/");
                    $ua->setUcPlatform(substr($item, 0, $index));
                    $ua->setUcPlatformVersion(substr($item, $index + 1));
                }else if(stripos($descs[$j], GlobalConstant::MOBILE) !== false){
                    $ua->setMobileMode(true);
                }
            }
        }catch (Exception $e) { 
            error_log($e->getMessage());
        }
    }
    
    private static function parseIUC($userAgent, $ua) {
        $ua->setVersion(GlobalConstant::VERSION_1);
        $ua->setUcPlatform(GlobalConstant::U2);
        $ua->setUcPlatformVersion("1.0.0");
        $ua->setProduct(GlobalConstant::UCBROWSER);
        
        $companyVersion = "";
        $platform = "";
        $encryption = "";
        $os = "";
        $language = "";
        $device = "";
        $rendering = "";
        $product = "";
        $mobile = "";
        $other = "";
        $startIndex = strpos($userAgent, "(");
        
        // 获取Company参数
        if($startIndex !== false){
            $companyVersion = substr($userAgent, 0, $startIndex);
        }
        
        $endIndex = strpos($userAgent, ")");
        
        $deviceInfo =  substr($userAgent, $startIndex + 1, $endIndex - $startIndex - 1);
        $deviceInfos = explode(";", $deviceInfo);
        try{
           // platform = deviceInfos[0].trim();
            $ua->setPlatform("iOS");
            
            $encryption = trim($deviceInfos[0]);
            $ua->setEncryption($encryption);
            
            $os = trim($deviceInfos[1]);
            UserAgentFactory::setOsInfo($ua, $os);
            
            $language = trim($deviceInfos[2]);
            $ua->setLanguage($language);
            
//            if(deviceInfos.length >= 5){
//                device = deviceInfos[4].trim();
                $ua->setDevice("iPhone");
//            }else{
//                
//            }
            
        }catch (Exception $e) { 
            error_log($e->getMessage());
        }
        
        try{
            $desc = trim(substr($userAgent, $endIndex + 1));
            $descs = explode(" ", $desc);
            $index = -1; 
  
            $descsLength = count($descs);
            for($j = 0; $j < $descsLength; $j++){ 
                if(stripos($descs[$j], GlobalConstant::UCBROWSER) !== false){
                    $ua->setProduct(GlobalConstant::UCBROWSER);
                    $endIndex =  strpos($descs[$j], "/");
                    $ua->setProductVersion(substr($descs[$j], 9, $endIndex - 9));                  
                }else if(stripos($descs[$j], GlobalConstant::U2) !== false || stripos($descs[$j], GlobalConstant::U3) !== false){
                    $item = trim($descs[$j]);
                    $index = strpos($item, "/");
                    $ua->setUcPlatform(substr($item, 0, $index));
                    $ua->setUcPlatformVersion(substr($item, $index + 1));
                }else if(stripos($descs[$j], GlobalConstant::MOBILE) !== false){
                    $ua->setMobileMode(true);
                }else if(stripos($descs[$j], GlobalConstant::WEBKIT) !== false || stripos($descs[$j], GlobalConstant::MSIE) !== false){
                    $rendering = trim($descs[0]);
                    $index = strpos($rendering, "/");
                    $ua->setRenderingEngine(substr($rendering, 0, $index));
                    $ua->setRenderingEngineVersion(substr($rendering, $index + 1));
                }
            }
        }catch (Exception $e) { 
            error_log($e->getMessage());
        }
    }
    
    private static function parseJUC($userAgent, $ua) {
        $ua->setVersion(GlobalConstant::VERSION_1);
        $ua->setUcPlatform(GlobalConstant::U2);
        $ua->setUcPlatformVersion("1.0.0");
        $ua->setProduct(GlobalConstant::UCBROWSER);
        
        $companyVersion = "";
        $platform = "";
        $encryption = "";
        $os = "";
        $language = "";
        $device = "";
        $rendering = "";
        $product = "";
        $mobile = "";
        $other = "";
        $startIndex = strpos($userAgent, "(");
        
        // 获取Company参数
        if($startIndex !== false){
            $companyVersion = substr($userAgent, 0, $startIndex);
        }
        
        $endIndex = strpos($userAgent, ")");
        
        $deviceInfo =  substr($userAgent, $startIndex + 1, $endIndex - $startIndex - 1);
        $deviceInfos = explode(";", $deviceInfo);
        try{
            $platform = trim($deviceInfos[0]);
            $ua->setPlatform($platform);
            
            $encryption = trim($deviceInfos[1]);
            $ua->setEncryption($encryption);
            
            $os = trim($deviceInfos[2]);
            UserAgentFactory::setOsInfo($ua, $os);
            
            $language = trim($deviceInfos[3]);
            $ua->setLanguage($language);
            
            if(count($deviceInfos) >= 5){
                $device = trim($deviceInfos[4]);
                $ua->setDevice($device);
            }else{
                
            }
        }catch (Exception $e) { 
            error_log($e->getMessage());
        }
        
        try{
            $desc = trim(substr($userAgent, $endIndex + 1));
            $descs = explode(" ", $desc);
            $index = -1;
//            rendering = descs[0].trim();
//            int index = rendering.indexOf('/');
//            $ua->setRenderingEngine(rendering.substring(0, index));
//            $ua->setRenderingEngineVersion(rendering.substring(index + 1, rendering.length()));
//          
            $descsLength = count($descs);
            
            for($j = 0; $j < $descsLength; $j++){ 
                if(stripos($descs[$j], GlobalConstant::UCBROWSER) !== false || stripos($descs[$j], GlobalConstant::UCWEB) !== false){
                    $length = stripos($descs[$j], GlobalConstant::UCBROWSER) !== false ? strlen(GlobalConstant::UCBROWSER) : strlen(GlobalConstant::UCWEB);
                    $startIndex = $length;
                    if(substr($descs[$j], $length, 1) == "/"){
                        $endIndex = strpos($descs[$j], "/", $length + 1);
                        $startIndex++;
                    } else {
                        $endIndex = strpos($descs[$j], "/");
                    }
                    $ua->setProductVersion(substr($descs[$j], $startIndex, $endIndex - $startIndex));                  
                }else if(stripos($descs[$j], GlobalConstant::U2) !== false || stripos($descs[$j], GlobalConstant::U3) !== false){
                    $item = trim($descs[$j]);
                    $index = strpos($item, "/");
                    $ua->setUcPlatform(substr($item, 0, $index));
                    $ua->setUcPlatformVersion(substr($item, $index + 1));
                }else if(stripos($descs[$j], GlobalConstant::MOBILE) !== false){
                    $ua->setMobileMode(true);
                }else if(stripos($descs[$j], GlobalConstant::WEBKIT) !== false || stripos($descs[$j], GlobalConstant::MSIE) !== false){
                    $rendering = trim($descs[0]);
                    $index = strpos($rendering, "/");
                    $ua->setRenderingEngine(substr($rendering, 0, $index));
                    $ua->setRenderingEngineVersion(substr(rendering, $index + 1));
                }
            }
        }catch (Exception $e) { 
            error_log($e->getMessage());
        }
    }
}
?>
